/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yarolegovich.lovelydialog;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Paint;
import ohos.agp.utils.Rect;
import ohos.app.Context;

/**
 * 自定义component
 */
public abstract class BaseComponent extends Component {
    /**
     * restart
     */
    public final static int RESTART = 1;
    /**
     * reverse
     */
    public final static int REVERSE = 2;
    /**
     * animatorValue
     */
    public AnimatorValue animatorValue;
    private boolean isBack = false;


    public BaseComponent(Context context) {
        this(context, null);
    }

    public BaseComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public BaseComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initPaint();
    }

    /**
     * 启动动画
     */
    public void startAnim() {
        startAnim(1000);
    }

    /**
     * 启动动画
     *
     * @param time time
     */
    public void startAnim(int time) {
        stopAnim();
        startViewAnim(time);
    }

    /**
     * 启动动画
     *
     * @param time time
     * @return AnimatorValue
     */
    private AnimatorValue startViewAnim(long time) {
        animatorValue = new AnimatorValue();
        animatorValue.setDuration(time);
        animatorValue.setCurveType(Animator.CurveType.LINEAR);
        animatorValue.setLoopedCount(setAnimRepeatCount());
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                if (setAnimRepeatMode() == REVERSE) {
                    if (isBack) {
                        onAnimationUpdate(1 - value);
                    } else {
                        onAnimationUpdate(value);
                    }
                } else if (setAnimRepeatMode() == RESTART) {
                    onAnimationUpdate(value);
                } else {
                    onAnimationUpdate(value);
                }
            }
        });
        animatorValue.setLoopedListener(new Animator.LoopedListener() {
            @Override
            public void onRepeat(Animator animator) {
                onAnimationRepeat(animator);
                isBack = !isBack;
            }
        });
        if (!animatorValue.isRunning()) {
            ainmIsRunning();
            animatorValue.start();
        }
        return animatorValue;
    }

    /**
     * 停止动画
     */
    public void stopAnim() {
        if (animatorValue != null) {
            animatorValue.setLoopedCount(0);
            animatorValue.cancel();
            animatorValue.end();
            if (onStopAnim() == 0) {
                animatorValue.setLoopedCount(0);
                animatorValue.cancel();
                animatorValue.end();
            }

        }
    }

    /**
     * 初始化画笔
     */
    protected abstract void initPaint();

    /**
     * 动画更新时调用
     *
     * @param value value
     */
    protected abstract void onAnimationUpdate(float value);

    /**
     * 动画重复时调用
     *
     * @param animation animation
     */
    protected abstract void onAnimationRepeat(Animator animation);

    /**
     * 动画停止调用
     *
     * @return int
     */
    protected abstract int onStopAnim();

    /**
     * 设置动画重复模式
     *
     * @return int
     */
    protected abstract int setAnimRepeatMode();

    /**
     * 设置动画重复次数
     *
     * @return int
     */
    protected abstract int setAnimRepeatCount();

    /**
     * 动画是否运行中
     */
    protected abstract void ainmIsRunning();

    /**
     * dp转px
     *
     * @param dpValue dp
     * @return px
     */
    protected int dip2px(float dpValue) {
        return (int) (dpValue * getContext().getResourceManager().getDeviceCapability().screenDensity / 160);
    }

    /**
     * 获取字体长度
     *
     * @param paint panit
     * @param str   文字
     * @return 文字长度
     */
    protected float getFontlength(Paint paint, String str) {
        Rect rect = paint.getTextBounds(str);
        return rect.getWidth();
    }

    /**
     * 获取文字高度
     *
     * @param paint panit
     * @param str   文字
     * @return 高度
     */
    protected float getFontHeight(Paint paint, String str) {
        Rect rect = paint.getTextBounds(str);
        return rect.getHeight();
    }

    /**
     * 获取字体高度
     *
     * @param paint paint
     * @return flaot
     */
    protected float getFontHeight(Paint paint) {
        Paint.FontMetrics fm = paint.getFontMetrics();
        return fm.descent - fm.ascent;
    }

}
